﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using SUMO.MediaBrowser.Foundation;
using SUMO.MediaBrowser.ViewModel;
using USTF.Media.MediaService;

namespace SUMO.MediaBrowser.Controls
{
    public partial class ImageMediaItemThumbnail : UserControl
    {
        

        public HierarchicalMediaItem Model
        {
            get
            {
                return DataContext as HierarchicalMediaItem;
            }
        }

        public ImageMediaItemThumbnail()
        {
            InitializeComponent();
        }

        private void Image_MouseEnter(object sender, MouseEventArgs e)
        {
            Image i = (Image)sender;
            i.Opacity = 0.5;
        }

        private void Image_MouseLeave(object sender, MouseEventArgs e)
        {
            Image i = (Image)sender;
            i.Opacity = 1;
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            Image i = (Image)sender;
            i.Opacity = 1;

        }

        private void Image_ImageOpened(object sender, System.Windows.RoutedEventArgs e)
        {
            //hide loading animation
            Loading.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void EditBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
                IMediaItemEditor editWindow = MediaItemEditorController.GetEditingControlForMediaItem(this.Model);
                editWindow.Show();
                editWindow.Closing += new EventHandler<CancelEventArgs>(editWindow_Closing);
                editWindow.Closed += new EventHandler(editWindow_Closed);
        }

        void editWindow_Closed(object sender, EventArgs e)
        {
            IMediaItemEditor editWindow = (IMediaItemEditor)sender;

            //if user has indicated they would like to persist data
            if (editWindow.UserCommitResult != null && editWindow.UserCommitResult.Value == true)
            {
                //modify on our data
                editWindow.DataItem.Item.ItemData = editWindow.DataItem.WorkingData;
                RevertBtn.IsEnabled = editWindow.DataItem.Item.DataModified;
            }
        }

        void editWindow_Closing(object sender, CancelEventArgs e)
        {
            IMediaItemEditor editWindow = (IMediaItemEditor)sender;
            //this stub is here as a marker for any action that would require confirmation
            //as to whether the user really does want to persist the action in the
            //edit window.
        }

        private void RevertBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Model.RevertToOriginal();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (Model != null) {
                Thumb.Visibility = (Model.CurrentItem.MediaType == MediaType.Image) ? Visibility.Visible : Visibility.Collapsed;
                Folder.Visibility = (Model.CurrentItem.MediaType == MediaType.Folder) ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        private void Folder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }
    }
}


